import { http } from '@/utils/http';
import { useAuthStore } from '@/stores';
import type { AnyObj } from '@/types/global';


const baseURL = import.meta.env.VITE_HTTP_BASE_URL;

export const uploadFile = (tempFilePath : string) => {
	const authStore = useAuthStore();
	const token = authStore.profile?.access_token;
	return new Promise((resolve, reject) => {
		uni.uploadFile({
			url: `${baseURL}/wx/file/upload`,
			filePath: tempFilePath,
			name: "file",
			header: {
				'Authorization': `Bearer ${token}`,
			},
			success: (result) => {
				resolve(JSON.parse(result.data));
			},
			fail: (error) => {
				console.log('error', error);
				reject(error);
			}
		})
	})
}


export const getSupport = () => {
	return http<AnyObj>({
		method: 'GET',
		url: '/wx/account/getSupport'
	});
}


export const saveMessage = (data : AnyObj & {
	feedbackType : 1 | 2 // 1：留言；2：意见反馈
}) => {
	const authStore = useAuthStore();
	const wxUserId = authStore.profile?.userId;
	data.wxUserId = wxUserId;

	return http({
		method: 'POST',
		url: '/wx/feedback/saveMessage',
		data
	});
}


export const getCommentList = (data : AnyObj & { spuId : string; }) => {
	return http<AnyObj[]>({
		method: 'GET',
		url: '/wx/spuComment/list',
		data
	});
}

export const saveSpuComment = (data : AnyObj & { orderId : string; }) => {
	const authStore = useAuthStore();
	data.wxUserId = authStore.profile?.userId;
	return http<AnyObj[]>({
		method: 'POST',
		url: '/wx/spuComment/saveSpuComment',
		data
	});
}

export const saveBizComment = (data : AnyObj & { bizId : string; }) => {
	const authStore = useAuthStore();
	data.wxUserId = authStore.profile?.userId;
	return http<AnyObj[]>({
		method: 'POST',
		url: '/wx/feedback/saveBizFeedback',
		data
	});
}

export const getRegionTree = () => {
	return http<AnyObj[]>({
		method: 'GET',
		url: '/wx/region/regionTree'
	});
}

/**
 * 1=隐私政策条款
 * 2=服务条款
 * 3=服务规范
 */
export const getDoc = (docType : 1 | 2 | 3) => {
	return http<AnyObject>({
		method: 'GET',
		url: `/wx/document/${docType}`
	});
}